<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\api\lists;


use app\api\lists\BaseApiDataLists;
use app\common\model\card\CardGroup;
use app\common\lists\ListsSearchInterface;
use app\common\model\user\User;

/**
 * card列表
 * Class CardGroupLists
 * @package app\api\lists
 */
class CardGroupLists extends BaseApiDataLists implements ListsSearchInterface
{


    /**
     * @notes 设置搜索条件
     * @return \string[][]
     * @author likeadmin
     * @date 2025/04/09 19:15
     */
    public function setSearch(): array
    {
        return [
            '%like%' => ['name'],
        ];
    }


    /**
     * @notes 获取card列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author likeadmin
     * @date 2025/04/09 19:15
     */
    public function lists(): array
    {
      // 获取分页参数
      $page = max(1, intval($this->request->get('page_no', 1)));
      $limit = max(1, min(100, intval($this->request->get('page_size', 10))));
      $offset = ($page - 1) * $limit;
      
      $userIds = User::where('group_id',$this->userInfo['group_id'])->column('id');
      
      return CardGroup::where($this->searchWhere)
          ->field(['id', 'name', 'user_id'])
          ->limit($offset, $limit)  // 使用正确的分页参数
          ->whereIn('user_id',$userIds)
          ->order(['id' => 'desc'])
          ->select()
          ->toArray();
    }


    /**
     * @notes 获取card数量
     * @return int
     * @author likeadmin
     * @date 2025/04/09 19:15
     */
    public function count(): int
    {
        $userIds = User::where('group_id',$this->userInfo['group_id'])->column('id');
        return CardGroup::where($this->searchWhere)
            ->whereIn('user_id',$userIds)
            ->count();
    }

}